package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 新增套餐接口
     *
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R saveSetmeal(@RequestBody SetmealDto setmealDto) {
        setmealService.addSetmeal(setmealDto);
        return R.success("保存套餐成功");
    }

    /**
     * 套餐分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R setmealQueryPage(int page, int pageSize, String name) {
        //分页插件构造器
        IPage<Setmeal> pageSetmeal = new Page<>(page, pageSize);
        IPage<SetmealDto> pageDto = new Page<>(page, pageSize);

        //条件查询构造器
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper();
        qw.eq(name !=null, Setmeal::getName, name);
        qw.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(pageSetmeal,qw);

        long total = pageSetmeal.getTotal();
        List<Setmeal> records = pageSetmeal.getRecords();
        BeanUtils.copyProperties(pageSetmeal,pageDto,"records");
        List<SetmealDto> collect = records.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item, setmealDto);
            Long categoryId = item.getCategoryId();

            Category category = categoryService.getById(categoryId);
            String categoryName = category.getName();
            setmealDto.setCategoryName(categoryName);
            return setmealDto;
        }).collect(Collectors.toList());

        pageDto.setRecords(collect);
        return R.success(pageDto);

    }

    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    @Transactional
    public R deleteSetmeal(@RequestParam List<Long> ids){
        //条件查询构造器
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper();
        qw.in(ids!=null,Setmeal::getId,ids);
        qw.eq(Setmeal::getStatus,1);
        int count = setmealService.count(qw);
        if (count>0){
            throw new CustomException("套餐正在售卖中，不能删除");
        }
        //逻辑删除套餐
         setmealService.removeByIds(ids);

        //删除套餐对应的菜品数据:物理删除
        LambdaQueryWrapper<SetmealDish> qwd = new LambdaQueryWrapper();
        qwd.in(ids != null,SetmealDish::getSetmealId,ids);
        setmealDishService.remove(qwd);
        return R.success("套餐删除成功");
    }

    /**
     * 起售/停售套餐
     * @param ids
     * @param status
     * @return
     */
    @PostMapping("/status/{status}")
    public R hastSaleSetmeal(@RequestParam List<Long> ids,@PathVariable Integer status){
    LambdaUpdateWrapper<Setmeal> qw = new LambdaUpdateWrapper();
    qw.in(ids!=null,Setmeal::getId,ids);
    qw.set(Setmeal::getStatus,status);
        boolean flag = setmealService.update(qw);
        return flag?R.success("操作成功"):R.error("操作失败");

    }

    /**
     * 修改套餐:根据套餐id查询套餐数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> updateQuerySetmeal(@PathVariable Long id){
        //根据套餐id查询套餐数据
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.eq(id!=null, Setmeal::getId,id);
        Setmeal setmealList = setmealService.getOne(qw);

        //根据套餐id查询套餐对应的菜品数据
        LambdaQueryWrapper<SetmealDish> qwd = new LambdaQueryWrapper<>();
        qwd.eq(id!=null, SetmealDish::getId,id);
        List<SetmealDish> setmealDishList = setmealDishService.list(qwd);

        //将套餐setmeal和套餐菜品集合setmealDishList数据封装到SetmealDto并返回
        SetmealDto setmealdto = new SetmealDto();

        //拷贝setmeal到setmealDto中
        BeanUtils.copyProperties(setmealList,setmealdto);

        //将setmealDishList设置到setmealDto中
        setmealdto.setSetmealDishes(setmealDishList);
        return R.success(setmealdto);
    }

    /**
     * 修改套餐
     * @param setmealDto
     * @return
     */
    @PutMapping
    public R updateSetmeal(@RequestBody SetmealDto setmealDto){
        setmealService.updateSetmeal(setmealDto);
        return R.success("修改套餐成功");
    }

    /**
     * 根据套餐分类id查询该套餐分类下的套餐
     * @param id
     * @param status
     * @return
     */
    @GetMapping("/list")
    public R<List<Setmeal>> getSetmealByCategoryId(Long id,Integer status){
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.eq(id!=null,Setmeal::getCategoryId,id);
        qw.eq(status !=null,Setmeal::getStatus,status);
        List<Setmeal> list = setmealService.list(qw);
        return R.success(list);
    }
}
